/**
 * @Created by pangzg pangzg2@yusys.com.cn on 2020-4-14 10:37:16.
 * @updated by
 * @description 翻牌日期监控
 */
define(function (require, exports) {
	/**
	 * 页面加载完成时触发
	 * @param hashCode 路由ID
	 * @param data 传递数据对象
	 * @param cite 页面站点信息
	 */
	exports.ready = function (hashCode, data, cite) {
		yufp.custom.vue({
			el: cite.el,
			data: function () {
				/**检验偏移量必须《=0*/
				var offsetNum = function (rule, value, callback) {

					if (value) {
						if (isNaN(value)) {
							callback(new Error('偏移量必须<=0'));
						} else {
							if (Number(value) > 0) {
								callback(new Error('偏移量必须<=0'));
							} else {
								callback();
							}
						}
					} else {
						callback();
					}
				};
				return {
					searchFormdata: {},
					tbHeight:'0',
					//导入导出的data  start
					importFnButton: !yufp.session.checkCtrl('importFn'),
					exportFnButton: !yufp.session.checkCtrl('exportFn'),
					upLoadDialogVisible: false,//是否显示导入页面
					upLoadUrl: yufp.service.getUrl({ url: "api/file/provider/uploadfile" }),//导入的url
					appUploadDatas: [],//文件列表
					showHeader: false,//是否显示错误表头
					errshow: false,//显示导入的错误信息，默认不显示
					excelloading: false,//导入的loading
					errdata: [],//导入的错误信息
					multipleFalg: false,//是否允许多文件
					//导入导出的data  end
					//权限控制相关
					updateFnShow: !yufp.session.checkCtrl('modifyFn'),
					addFnShow: !yufp.session.checkCtrl('addFn'),
					deleteFnShow: !yufp.session.checkCtrl('deleteFn'),
					exportFnShow: !yufp.session.checkCtrl('exportFn'),
					importFnShow: !yufp.session.checkCtrl('importFn'),
					dataUrl: '/api/use/monitor/flop/queryList',
					formdata: {},
					dialogVisible: false,
					formDisabled: false,
					viewType: 'DETAIL',
					viewTitle: '新增翻牌日期',
					saveBtnShow: true,
					addFlag: true,
					offsetRules: [
						{ required: true, message: '必填项', trigger: 'blur' },
						{ validator: offsetNum, trigger: 'blur' }
					],
					exportLoading: false
				};
			},
			mounted:function(){
				var _this = this;
				//测试
				_this.$nextTick(function(){
						var tableBox = _this.$refs.tableBox;
						let a = tableBox.getBoundingClientRect().top + 28;
						var theight = `calc(100vh - ${a}px - 64px)`;
						_this.tbHeight = theight;
				});
			},
			methods: {
				//点击导出模板操作
				exportDemo: function () {
					var _this = this;
					var condition = { busiType: "TXDATE" };
					var search = _this.searchFormdata;
					yufp.util.download("api/use/excel/exportTempExcel?condition="
						+ encodeURI(JSON.stringify(condition)));
				},
				//点击导出操作
				exportFlowFn: function () {
					var _this = this;
					var condition = { busiType: "TXDATE" };
					var search = _this.searchFormdata;

					condition.groupCode = search.groupCode;
					condition.txdate = search.txdate;
					condition.txdateOffset = search.txdateOffset;
					_this.exportLoading = true;

					var selections = _this.$refs.refTable.selections;
					if (selections.length > 0) {
						condition.dataList = _this.$refs.refTable.selections;
					}

					yufp.service.request({
						  method: 'POST',
						  url: '/api/use/excel/exportExcelForWeb',
						data: { condition: condition },
						callback: function (code, message, response) {
							_this.exportLoading = false;
							if (response.code === 0) {
								yufp.util.download("api/use/excel/downLoadExcel?filePath=" + response.data.allFilePath + "&fileName=" + response.data.fileName);
							} else {
								this.$message.error('导出失败');
							}
						}
					});
				},
				importFlowFn: function () { //点击导入操作
					this.upLoadDialogVisible = true;
					this.$nextTick(function () {
						this.$refs['my-upload'].clearFiles();
						this.errshow = false;
						this.errdata = [];
					})
				},
				/**
				 *导入前的检验操作操作
				 */
				checkUpload: function (file) {
					//alert(file);

					var isLt10M = file.size / 1024 / 1024 < 100;
					if (!isLt10M) {
						this.$message.error('上传文件大小不能超过 100MB!');
					}
					var index = file.name.lastIndexOf('.');
					var ext = file.name.substr(index + 1);

					var suffixValid = ('xlsx' === ext || 'xls' === ext);
					if (!suffixValid) {
						this.$message.error('上传文件的格式必须为excel格式');
						return false;
					}
					return isLt10M && suffixValid;
				},
				successUpload: function (response, file, fileList) { //导入成功后执行
					/*	console.log('上传成功！')
						this.$refs['my-upload'].clearFiles();*/


					const _this = this;
					const param = {};
					/* 					_this.$loading({
											target:'.load-target',
											body:true
										}) */
					_this.excelloading = true;
					param.busiType = "TXDATE";
					param.excelUpLoadFile = response.data;
					yufp.service.request({
						method: 'POST',
						url: '/api/use/excel/importExcel',
						data: param,
						timeout: 1800000,
						callback: function (code, message, response) {
							_this.excelloading = false;
							if (response.code == 0 && response.data.errorInfos.length == 0 && response.data.impStatus != '失败') {
								_this.$refs['my-upload'].clearFiles();
								_this.errshow = false;
								_this.errdata = [];
								_this.upLoadDialogVisible = false;
								_this.$message('导入成功!');
								//同时重新查询列表

								var param = {
									condition: JSON.stringify(_this.searchFormdata)
								};
								// _this.$refs.refTable.remoteData(param);
								_this.refresh(param);
							} else {
								_this.errshow = true;
								_this.$refs['my-upload'].clearFiles();
								_this.errdata = [];
								if (response.data && response.data.errorInfos.length > 0 && response.data.errorInfos[0].errorList) {
									response.data.errorInfos[0].errorList.map(function (item, index) {
										_this.errdata.push({
											"errMsg": item
										})
										console.log('errdata', _this.errdata)
									})
								} else {
									_this.errdata.push({
										"errMsg": response.message
									})
								}

							}
						}
					});
				},
				/**
				 * 单击选择
				 */
				cellClick: function (row, event, column, index) {
					var _this = this;
					if (row) {
						_this.$refs.refTable.toggleRowSelection(row);
					}
				},
				/**
				 * 取消
				 */
				cancelFn: function () {
					var _this = this;
					_this.dialogVisible = false;
				},
				/**
				 * 保存
				 */
				saveFn: function () {
					var _this = this;
					var url = '';
					var model = {};
					yufp.clone(_this.formdata, model);
					var validate = false;
					_this.$refs.refForm.validate(function (valid) {
						validate = valid;
					});
					if (!validate) {
						return;
					}
					if (_this.addFlag) {
						url = 'api/use/monitor/flop/add';
					} else {
						url = '/api/use/monitor/flop/update';
					}
					// 向后台发送保存请求
					yufp.service.request({
						method: 'POST',
						url: url,
						data: model,
						callback: function (code, message, response) {
							if (response.code == 0) {
								_this.refresh();
								_this.$message('操作成功');
								_this.dialogVisible = false;
							} else {
								_this.$message(response.message);
							}
						}
					});
				},
				refresh:function(param){
					this.$refs.refTable.remoteData(param);
					this.$refs.refTable.clearSelection();
				},
				/**
				 * 控制保存按钮、xdialog、表单的状态
				 * @param viewType 表单类型
				 * @param editable 可编辑,默认false
				 */
				switchStatus: function (viewType, editable) {
					var _this = this;
					_this.viewType = viewType;
					_this.saveBtnShow = editable;
					_this.dialogVisible = true;
					_this.formDisabled = !editable;
				},
				/**
				 * 修改
				 */
				modifyFn: function () {
					var _this = this;
					_this.viewTitle = '翻牌日期修改';
					if (_this.$refs.refTable.selections.length != 1) {
						_this.$message({
							message: '请先选择一条记录',
							type: 'warning', duration: 6000
						});
						return;
					}
					_this.switchStatus('EDIT', true);
					_this.addFlag = false;
					_this.$nextTick(function () {
						_this.$refs.refForm.resetFields();
						var obj = _this.$refs.refTable.selections[0];
						yufp.clone(obj, _this.formdata);
					});
				},
				/**
				 * 修改
				 */
				deleteFn: function () {
					var _this = this;
					if (_this.$refs.refTable.selections.length != 1) {
						_this.$message({
							message: '请先选择一条记录',
							type: 'warning', duration: 6000
						});
						return;
					} else {
						_this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
							confirmButtonText: '确定',
							cancelButtonText: '取消',
							type: 'warning', duration: 6000,
							center: true,
							callback: function (action) {
								if (action === 'confirm') {
									var row = _this.$refs.refTable.selections[0];
									yufp.service.request({
										method: 'DELETE',
										url: 'api/use/monitor/flop/delete',
										data: row,
										callback: function (code, message, response) {
											if (response.code == 0) {
												_this.refresh();
												_this.$message('操作成功');
												_this.dialogVisible = false;
											} else {
												_this.$message('操作失败');
											}
										}
									});
								}
							}
						})
					}

				},
				/**
				 * 新增
				 */
				addFn: function () {
					var _this = this;
					_this.viewTitle = '翻牌日期新增';
					_this.switchStatus('ADD', true);
					_this.addFlag = true;
					_this.$nextTick(function () {
						_this.$refs.refForm.resetFields();
					});
				}
			}
		});
	};
});
